import json
from datetime import datetime

class GlBookDetailRequestParams:
    """
    用于构建财务账簿明细查询请求参数的类
    """

    # 固定参数（已按需求修改）
    BASE_PARAMS = {
        "init_date_start": "2025-08-01T00:00:00.000Z",
        "noOccurAndNoEndAmountShow": "0",
        "endAccountGrade": "5",
        "stopAccountNotShow": "0",
        "noOccurNotShow": "0",
        "accountIdList": [],
        "currencyId": "0",
        "docSummary": "",
        "assitForm": {},
        "beginAccountGrade": "1",
        "noDataNoDisplay": "1",
        "showSpec": "0",
        "init_date_end": "2025-08-01T00:00:00.000Z",
        "accountCode": "16010101",
        "onlyShowEndAccount": "false",
        "noEndAmountNotShow": "0",
        "beginDate": "2024-03",
        "endDate": "2024-03",
        "isCalcMulti": False,
        "isCalcQuantity": False,
        "accountSelectType": 0,
        "page": {
            "currentPage": 1,
            "pageSize": 1000
        },
        "showCalcAccountParam": "0"
    }

    def __init__(self, beginDate: str):
        """
        初始化请求参数

        :param beginDate: 开始日期（格式: YYYY-MM，必填）
        """
        self.params = self.BASE_PARAMS.copy()
        self.params["beginDate"] = beginDate
        self.params["endDate"] = beginDate  # 默认endDate等于beginDate

    def set_enddate(self, endDate: str = None):
        """
        设置结束日期（默认使用beginDate作为endDate）

        :param endDate: 结束日期（格式: YYYY-MM），若未提供则使用beginDate
        """
        if endDate is None:
            endDate = self.params["beginDate"]
        self.params["endDate"] = endDate
        return self

    def set_account_code(self, accountCode: str):
        """
        设置科目代码

        :param accountCode: 科目代码（如"16010101"）
        """
        self.params["accountCode"] = accountCode
        return self

    def set_page_size(self, pageSize: int):
        """
        设置分页大小（默认1000）

        :param pageSize: 每页记录数
        """
        self.params["page"]["pageSize"] = pageSize
        return self

    def to_request_body(self):
        """
        构建完整的请求体

        :return: 包含interfaceName, methodName, methodParam的字典
        """
        method_param_str = json.dumps([self.params])

        return {
            "interfaceName": "com.ttk.gl.facade.IGlBookDetailFacade",
            "methodName": "queryForAccount",
            "methodParam": method_param_str
        }

    def __str__(self):
        """返回参数的字符串表示（用于调试）"""
        return f"GlBookDetailRequestParams(beginDate={self.params['beginDate']}, endDate={self.params['endDate']})"